"""
/*
 * Copyright 2011 OpenWAF.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
 """
from xml.dom.minidom import parse, parseString
import xml.dom
import os
import sys

class WAFConfig:
	compiled_js_file	=None
	source_path			= None
	target_path			= None
	lib_paths			= []
	compile_mode  		= "test" #deply
	target_ide	  		= "netbeans" #"eclipse"
	target_server 		= "apache" #glassfish
	proxy_servlet_url 	= "/WAFProxy" 
	obfuscate			= False
	minify				= False
	add_gen_code_line_prefix = True
	gen_code_line_prefix= "/** DNM **/"
	remove_unused_code  = False
	mark_unused_code	= False
	unused_code_marker  = "/** UNUSED **/"
	enable_array_getter_setter = True
	getter_setter_optimization = False
	waf_root_object_name	= "$w"
	aggregate_strings	= False
	compatible_with_ie_6_7 = True
	style_sheets=[]
	@staticmethod
	def getProxyURL():
		return WAFConfig.proxy_servlet_url
	@staticmethod
	def getCompiledJSFileName():
		return WAFConfig.compiled_js_file
	@staticmethod
	def getSourcePath():
		return WAFConfig.source_path
	@staticmethod
	def getTargetPath():
		return WAFConfig.target_path
	@staticmethod
	def getLibPaths():
		return WAFConfig.lib_paths
	@staticmethod
	def isCompatibleWithIE67():
		return WAFConfig.compatible_with_ie_6_7
	@staticmethod
	def isMinify():
		return WAFConfig.minify
	@staticmethod
	def isDeploy():
		return WAFConfig.compile_mode=="deploy"
	@staticmethod
	def isTest():
		return not WAFConfig.isDeploy()
	
	@staticmethod
	def getWAFRootObjectName():
		return WAFConfig.waf_root_object_name	
	@staticmethod
	def isStringAggregateEnabled():
		return WAFConfig.aggregate_strings

	@staticmethod
	def isRemoveUnusedCode():
		return WAFConfig.remove_unused_code	
	@staticmethod
	def getBool(value):	return value.lower()=="true"

		
	@staticmethod
	def read(fpath):
		if not os.path.exists(fpath):
			print "OpenWAF_config.xml file not found"
			sys.exit(0)
		f=open(fpath);
		root= parse(f).childNodes[0]
		for node in root.childNodes:
			if node.nodeType!=node.ELEMENT_NODE:continue
			if node.nodeName=="compiled-js-file":WAFConfig.compiled_js_file =node.firstChild.nodeValue.encode('ascii','ignore')
			elif node.nodeName=="target-path":WAFConfig.target_path =node.firstChild.nodeValue.encode('ascii','ignore')
			elif node.nodeName=="lib-paths":
				for cnode in node.childNodes:
					if cnode.nodeType!=cnode.ELEMENT_NODE:continue
					if cnode.nodeName=="path": 
						path=cnode.firstChild.nodeValue.encode('ascii','ignore')
						WAFConfig.lib_paths.append(path)					
			elif node.nodeName=="target-ide": WAFConfig.target_ide=node.firstChild.nodeValue.encode('ascii','ignore')
			elif node.nodeName=="target-web-app-server": WAFConfig.target_server =node.firstChild.nodeValue.encode('ascii','ignore')
			elif node.nodeName=="compile-mode": WAFConfig.compile_mode=node.firstChild.nodeValue.encode('ascii','ignore')
			elif node.nodeName=="proxy-servlet-url": WAFConfig.proxy_servlet_url =node.firstChild.nodeValue.encode('ascii','ignore')
			elif node.nodeName=="style-sheets":
				for cnode in node.childNodes:
					if cnode.nodeType!=cnode.ELEMENT_NODE:continue
					if cnode.nodeName=="style-sheet":
						name=cnode.firstChild.nodeValue.encode('ascii','ignore')
						if len(name.strip())==0:continue
						WAFConfig.style_sheets.append(name)
			elif node.nodeName=="code-gen-settings":
				if node.getAttribute("mode")!=WAFConfig.compile_mode:continue
				for cnode in node.childNodes:
					if cnode.nodeType!=cnode.ELEMENT_NODE:continue
					if cnode.nodeName=="obfuscate": WAFConfig.obfuscate=WAFConfig.getBool(cnode.firstChild.nodeValue.encode('ascii','ignore'))
					if cnode.nodeName=="minify": WAFConfig.minify=WAFConfig.getBool(cnode.firstChild.nodeValue.encode('ascii','ignore'))
					elif cnode.nodeName=="add-prefix-to-gen-code": WAFConfig.add_gen_code_line_prefix =WAFConfig.getBool(cnode.firstChild.nodeValue.encode('ascii','ignore'))
					elif cnode.nodeName=="waf-root-object-name": WAFConfig.waf_root_object_name=cnode.firstChild.nodeValue.encode('ascii','ignore')
					elif cnode.nodeName=="gen-line-code-prefix": WAFConfig.gen_code_line_prefix=cnode.firstChild.nodeValue.encode('ascii','ignore')
					elif cnode.nodeName=="remove-unused-code": WAFConfig.remove_unused_code =WAFConfig.getBool(cnode.firstChild.nodeValue.encode('ascii','ignore'))
					elif cnode.nodeName=="mark-unused-code": WAFConfig.mark_unused_code =WAFConfig.getBool(cnode.firstChild.nodeValue.encode('ascii','ignore'))
					elif cnode.nodeName=="unused-code-marker": WAFConfig.unused_code_marker=cnode.firstChild.nodeValue.encode('ascii','ignore')
					elif cnode.nodeName=="enable-array-getter-setter": WAFConfig.enable_array_getter_setter =WAFConfig.getBool(cnode.firstChild.nodeValue.encode('ascii','ignore'))
					elif cnode.nodeName=="getter-setter-optimization": WAFConfig.getter_setter_optimization =WAFConfig.getBool(cnode.firstChild.nodeValue.encode('ascii','ignore'))
					elif cnode.nodeName=="aggregate-strings":WAFConfig.aggregate_strings=WAFConfig.getBool(cnode.firstChild.nodeValue.encode('ascii','ignore'))
					elif cnode.nodeName=="compatible-with-ie6": 
						WAFConfig.compatible_with_ie_6_7 =WAFConfig.getBool(cnode.firstChild.nodeValue.encode('ascii','ignore'))
			
			
			#print dir(node)

if __name__=="__main__":
	WAFConfig.read('D:/Personal/OpenWAFPublic/projects/ecllipse_pyjc/OpenWAF_Compiler/src/OpenWAF_Config.xml')
	
			
		 
		
		
	
	
	
	
	